package com.cyj.crm02.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cyj.crm02.entity.Employee;
import com.cyj.crm02.entity.Role;
import com.cyj.crm02.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private RoleService roleService;

    @GetMapping("/list")
    public List<Role> roles(){
        return roleService.list();
    }

    /**
     *  查询一页角色信息
     *
     */
    @GetMapping("/pageList")
    public Map getList(Integer page,Integer limit ){
        Map<String,Object> map = new HashMap<String,Object> ();
        Page<Role> page1 = new Page<Role>();
        page1.setSize(limit);
        page1.setCurrent(page);

        IPage<Role> iPage = roleService.selectRoleList(page1);

        map.put("msg","查询情况");
        map.put("count",iPage.getTotal());
        map.put("data",iPage.getRecords());
        map.put("code",0);

        System.out.println(map);
        return map;
    }

    /**
     *  删除角色
     */
    @DeleteMapping("/del/{roleId}")
    public Map del(@PathVariable Integer roleId){
        Role role = new Role();
        role.setRoleId(roleId);
        role.setIsDel(1);
        Map<String,Object> result = new HashMap<String,Object>();
        int num = 0;
        boolean flag = roleService.updateById(role);
        if (flag==true){
            num = 1;
            result.put("msg","删除成功");
        }else {
            result.put("msg","删除失败");
        }
        result.put("state",num);
        return result;
    }

    /**
     *  新增角色
     */
    @PostMapping(value="/add",produces = "application/json;charset=UTF-8")
    public Map add(Role role,Integer departmentId){
        Map<String,Object> result = new HashMap<String,Object>();
        int num = roleService.addRoleDepartment(role,departmentId);
        result.put("state",num);
        if(num ==1){
            result.put("msg","添加成功");
        }else {
            result.put("msg","添加失败");
        }
        return result;
    }

    /**
     *  赋予角色权限
     */
    @PostMapping(value="/addPermission",produces = "application/json;charset=UTF-8")
    public Map addPermission(Role role,Integer permissionId){
        Map<String,Object> result = new HashMap<String,Object>();
        int num = roleService.addRolePermission(role,permissionId);
        result.put("state",num);
        if(num ==1){
            result.put("msg","添加成功");
        }else {
            result.put("msg","添加失败");
        }
        return result;
    }

    /**
     *  修改角色
     */
    @PutMapping("/update")
    public Map edit(Role role,Integer departmentId){
        Map<String,Object> result = new HashMap<String,Object>();
        result.put("state",roleService.update(role,departmentId));
        return result;
    }
}
